﻿using System;
using System.ComponentModel;

namespace Nextensions.Basic
{
    public class OperationResult: IOperationResult
    {
        private const string OK = "Ok";

        public OperationResult() { }

        public OperationResult(bool Success): this(Success, OK)
        {
        }

        public OperationResult(bool Success, string Message)
        {
            this.Success = Success;
            this.Message = Message;
            this.Exception = null;
        }

        public OperationResult(Exception ex): this(ex, null)
        {
        }

        public OperationResult(Exception ex, string Message)
        {
            this.Success = false;
            if (!String.IsNullOrEmpty(Message))
            {
                this.Message = Message;
            }
            else
            {
                this.Message = ex == null ? "[NULL]" : ex.Message;
            }
            this.Exception = ex;
        }

        public bool Success
        {
            get;set;
        }

        [DefaultValue("")]
        public string Message
        {
            get;set;
        }

        [DefaultValue(null)]
        public Exception Exception
        {
            get;set;
        }

        public override string ToString()
        {
            return string.Format("The operation was {0}successful: {1}", Success ? "" : "not ", Message);
        }
    }
}
